home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / infosrvr / dev / www_talk.930 / 000344_marca@wintermu….ncsa.uiuc.edu _Thu Nov 19 04:29:11 1992.msg < prev    next >
Internet Message Format  |  1994-01-24  |  24KB

  1. Return-Path: <marca@wintermute.ncsa.uiuc.edu>
  2. Received: from dxmint.cern.ch by  nxoc01.cern.ch  (NeXT-1.0 (From Sendmail 5.52)/NeXT-2.0)
  3.     id AA12795; Thu, 19 Nov 92 04:29:11 MET
  4. Received: by dxmint.cern.ch (dxcern) (5.57/3.14)
  5.     id AA20231; Thu, 19 Nov 92 04:41:41 +0100
  6. Received: from wintermute.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA11904
  7.   (5.65a/IDA-1.4.2 for www-talk@nxoc01.cern.ch); Wed, 18 Nov 92 21:41:25 -0600
  8. Received: by wintermute.ncsa.uiuc.edu (920110.SGI/911001.SGI)
  9.     for @newton.ncsa.uiuc.edu:www-talk@nxoc01.cern.ch id AA09366; Wed, 18 Nov 92 21:42:37 -0800
  10. Date: Wed, 18 Nov 92 21:42:37 -0800
  11. From: marca@ncsa.uiuc.edu (Marc Andreessen)
  12. Message-Id: <9211190542.AA09366@wintermute.ncsa.uiuc.edu>
  13. To: www-talk@nxoc01.cern.ch
  14. Subject: html-mode.el update
  15.  
  16. Following is the latest html-mode -- a few bugs have been fixed, and
  17. some new features added.  If you use this, send me a note; if you have
  18. ideas on how it could specifically be made to be smarter and more
  19. helpful, let me know; if you don't think I should be mailing this to
  20. everyone on www-talk, let me know that too (there was quite a bit of
  21. initial interest in an html mode, so that's why I'm mailing it out
  22. now).
  23.  
  24. Cheers,
  25. Marc
  26.  
  27. --
  28. Marc Andreessen
  29. Software Development Group
  30. National Center for Supercomputing Applications
  31. marca@ncsa.uiuc.edu
  32.  
  33. ;;; --------------------------------------------------------------------------
  34. ;;; HTML mode, based on text mode.
  35. ;;; Copyright (C) 1985 Free Software Foundation, Inc.
  36. ;;; Copyright (C) 1992 National Center for Supercomputing Applications.
  37. ;;; NCSA modifications by Marc Andreessen (marca@ncsa.uiuc.edu).
  38. ;;;
  39. ;;; This program is free software; you can redistribute it and/or
  40. ;;; modify it under the terms of the GNU General Public License as
  41. ;;; published by the Free Software Foundation; either version 1, or
  42. ;;; (at your option) any later version.
  43. ;;;
  44. ;;; This program is distributed in the hope that it will be useful,
  45. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  47. ;;; General Public License for more details.
  48. ;;;
  49. ;;; You should have received a copy of the GNU General Public License
  50. ;;; along with GNU Emacs; see the file COPYING.  If not, write to the
  51. ;;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  52. ;;;
  53. ;;; -------------------------------- CONTENTS --------------------------------
  54. ;;;
  55. ;;; html-mode: Major mode for editing HTML hypertext documents.
  56. ;;; $Revision: 1.18 $
  57. ;;; $Date: 1992/11/19 05:39:25 $
  58. ;;;
  59. ;;; Canonical list of features:
  60. ;;;   See below.
  61. ;;;
  62. ;;; ------------------------------ INSTRUCTIONS ------------------------------
  63. ;;;
  64. ;;; Load html-mode.el before editing HTML documents.  html-mode will
  65. ;;; detect the ``.html'' suffix and activate itself appropriately.
  66. ;;;
  67. ;;; You are assumed to be at least somewhat familiar with HTML format.
  68. ;;; If you aren't, read about it first (see below).
  69. ;;;
  70. ;;; Here are key sequences and corresponding commands:
  71. ;;;
  72. ;;; NORMAL COMMANDS:
  73. ;;;
  74. ;;; C-c a         html-add-address
  75. ;;;   Open an address element.
  76. ;;;
  77. ;;; C-c d         html-add-definition-list
  78. ;;;   Open a definition list.  The initial entry is created for you.
  79. ;;;   To create subsequent entries, use 'C-c e'.
  80. ;;;
  81. ;;; C-c e         html-add-definition-entry
  82. ;;;   Add a new definition entry in a definition list.  You are
  83. ;;;   assumed to be inside a definition list (specifically, at the end
  84. ;;;   of another definition entry).
  85. ;;;
  86. ;;; C-c h         html-add-header
  87. ;;;   Add a header.  You are prompted for size (1 is biggest, 2 is
  88. ;;;   next biggest) and header contents.
  89. ;;;
  90. ;;; C-c i         html-add-list-or-menu-item
  91. ;;;   Add a new list or menu item in a list or menu.  You are assumed
  92. ;;;   to be inside a list or menu (specifically, at the end of another
  93. ;;;   item).
  94. ;;;
  95. ;;; C-c l         html-add-normal-link
  96. ;;;   Add a link.  You will be prompted for the link (any string;
  97. ;;;   e.g., http://foo.bar/argh/blagh).  The cursor will be left where
  98. ;;;   you can type the text that will represent the link in the
  99. ;;;   document.
  100. ;;;
  101. ;;; C-c m         html-add-menu
  102. ;;;   Open a menu.  The initial item is created for you.  To create
  103. ;;;   additional items, use 'C-c i'.
  104. ;;;
  105. ;;; C-c p         html-add-paragraph-separator
  106. ;;;   Use this command at the end of each paragraph.
  107. ;;;
  108. ;;; C-c s         html-add-list
  109. ;;;   Open a list.  The initial item is created for you.  To create
  110. ;;;   additional items, use 'C-c i'.
  111. ;;;
  112. ;;; C-c t         html-add-title
  113. ;;;   Add a title to the document.  You will be prompted for the
  114. ;;;   contents of the title.  If a title already exists at the very
  115. ;;;   top of the document, the existing contents will be replaced.
  116. ;;;
  117. ;;; C-c x         html-add-plaintext
  118. ;;;   Add plaintext.  The cursor will be positioned where you can type
  119. ;;;   plaintext (or insert another file, or whatever).
  120. ;;;
  121. ;;; COMMANDS THAT OPERATE ON THE CURRENT REGION:
  122. ;;;
  123. ;;; C-c C-r l     html-add-normal-link-to-region
  124. ;;;   Add a link that will be represented by the current region.  You
  125. ;;;   will be prompted for the link (any string, as with
  126. ;;;   html-add-normal-link).
  127. ;;;
  128. ;;; C-c C-r r     html-add-reference-to-region
  129. ;;;   Add a reference (a link that does not reference anything) that
  130. ;;;   will be represented by the current region.  You will be prompted
  131. ;;;   for the name of the link; if you just press RET, a numeric name
  132. ;;;   will be created for you.
  133. ;;;
  134. ;;; SPECIAL COMMANDS:
  135. ;;;
  136. ;;; <, >, &
  137. ;;;   These are overridden to output <, >, and &
  138. ;;;   respectively.  The real characters <, >, and & can be entered
  139. ;;;   into the text either by prepending 'C-c' to the character or by
  140. ;;;   using the Emacs quoted-insert (C-q) command.
  141. ;;;
  142. ;;; C-c <, C-c >, C-c &
  143. ;;;   See '< > &' above.
  144. ;;;
  145. ;;; NOTE: The key bindings above are what I find to be useful and easy
  146. ;;; to remember.  If you have ideas on how to make them easier to
  147. ;;; handle for yourself or other people, please let me know.
  148. ;;; (Ideally, these commands all go in menus; to that end, someday
  149. ;;; soon I'll add a Lucid Emacs menu to html-mode.)
  150. ;;;
  151. ;;; ---------------------------- ADDITIONAL NOTES ----------------------------
  152. ;;;
  153. ;;; If you are running Epoch or Lucid Emacs, highlighting will be used
  154. ;;; to deemphasize HTML message elements as they are created.  You can
  155. ;;; turn this off; see the source code.
  156. ;;;
  157. ;;; To reorder all of the link NAME fields in your message (in order
  158. ;;; of their occurrence in the text), use:
  159. ;;;
  160. ;;; html-reorder-numeric-names
  161. ;;;   Reorder the NAME fields for links in the current buffer.  The
  162. ;;;   new ordering starts at 1 and increases monotonically through the
  163. ;;;   buffer.  If optional arg REORDER-NON-NUMERIC is non-nil, then
  164. ;;;   non-numeric NAME's will also be numbered, else they won't.
  165. ;;;
  166. ;;; -------------------------------- GOTCHAS ---------------------------------
  167. ;;;
  168. ;;; HTML documents can be tricky.  html-mode is not smart enough to
  169. ;;; enforce correctness or sanity, so you have to do that yourself.
  170. ;;;
  171. ;;; In particular, html-mode is smart enough to generate unique
  172. ;;; numeric NAME id's for all links that were (1) created via an
  173. ;;; html-mode command or (2) present in the file when it was loaded.
  174. ;;; Any other links (e.g. links added via Emacs cut and paste) may
  175. ;;; have ID's that conflict with ID's html-mode generates.  You must
  176. ;;; watch for this and fix it when appropriate; otherwise, your
  177. ;;; hypertext document will not work correctly.
  178. ;;;
  179. ;;; html-reorder-numeric-names can be used to reset all of the NAME
  180. ;;; id's in a document to an ordered sequence; this will also give
  181. ;;; html-mode a chance to look over the document and figure out what
  182. ;;; new links should be named to be unique.
  183. ;;;
  184. ;;; ------------------------- WHAT HTML-MODE IS NOT --------------------------
  185. ;;;
  186. ;;; html-mode is not a mode for *browsing* HTML documents.  In
  187. ;;; particular, html-mode provides no hypertext capabilities.  There
  188. ;;; is a clear need for an HTML browser; if you write one, let me
  189. ;;; know.
  190. ;;;
  191. ;;; ------------------------------ WHAT HTML IS ------------------------------
  192. ;;;
  193. ;;; HTML (HyperText Markup Language) is a format for hypertext
  194. ;;; documents.  For more information on HTML, telnet to info.cern.ch.
  195. ;;;
  196. ;;; --------------------------------------------------------------------------
  197. ;;; LCD Archive Entry:
  198. ;;; html-mode|Marc Andreessen|marca@ncsa.uiuc.edu|
  199. ;;; Major mode for editing HTML hypertext files.|
  200. ;;; $Date: 1992/11/19 05:39:25 $|$Revision: 1.18 $|~/modes/html-mode.el.Z|
  201. ;;; --------------------------------------------------------------------------
  202.  
  203. ;; TODO:
  204. ;; sgml-mode stuff.
  205.  
  206. (provide 'html-mode)
  207.  
  208. ;;; ------------------------------- variables --------------------------------
  209.  
  210. (defvar html-use-highlighting t
  211.   "*Flag to use highlighting for HTML directives in Epoch or Lucid Emacs; 
  212. if non-NIL, highlighting will be used.")
  213.  
  214. (defvar html-deemphasize-color "grey80"
  215.   "*Color for de-highlighting HTML directives in Epoch or Lucid Emacs.")
  216.  
  217. (defvar html-emphasize-color "yellow"
  218.   "*Color for highlighting HTML something-or-others in Epoch or Lucid Emacs.")
  219.  
  220. ;;; --------------------------------- setup ----------------------------------
  221.  
  222. (defvar html-mode-syntax-table nil
  223.   "Syntax table used while in html mode.")
  224.  
  225. (defvar html-mode-abbrev-table nil
  226.   "Abbrev table used while in html mode.")
  227. (define-abbrev-table 'html-mode-abbrev-table ())
  228.  
  229. (if html-mode-syntax-table
  230.     ()
  231.   (setq html-mode-syntax-table (make-syntax-table))
  232.   (modify-syntax-entry ?\" ".   " html-mode-syntax-table)
  233.   (modify-syntax-entry ?\\ ".   " html-mode-syntax-table)
  234.   (modify-syntax-entry ?' "w   " html-mode-syntax-table))
  235.  
  236. (defvar html-mode-map nil "")
  237. (if html-mode-map
  238.     ()
  239.   (setq html-mode-map (make-sparse-keymap))
  240.   (define-key html-mode-map "\t" 'tab-to-tab-stop)
  241.   (define-key html-mode-map "\C-ca" 'html-add-address)
  242.   (define-key html-mode-map "\C-cd" 'html-add-definition-list)
  243.   (define-key html-mode-map "\C-ce" 'html-add-definition-entry)
  244.   (define-key html-mode-map "\C-ch" 'html-add-header)
  245.   (define-key html-mode-map "\C-ci" 'html-add-list-or-menu-item)
  246.   (define-key html-mode-map "\C-cl" 'html-add-normal-link)
  247.   (define-key html-mode-map "\C-cm" 'html-add-menu)
  248.   (define-key html-mode-map "\C-cp" 'html-add-paragraph-separator)
  249.   (define-key html-mode-map "\C-cs" 'html-add-list)
  250.   (define-key html-mode-map "\C-ct" 'html-add-title)
  251.   (define-key html-mode-map "\C-cx" 'html-add-plaintext)
  252.   (define-key html-mode-map "<" 'html-less-than)
  253.   (define-key html-mode-map ">" 'html-greater-than)
  254.   (define-key html-mode-map "&" 'html-ampersand)
  255.   (define-key html-mode-map "\C-c<" 'html-real-less-than)
  256.   (define-key html-mode-map "\C-c>" 'html-real-greater-than)
  257.   (define-key html-mode-map "\C-c&" 'html-real-ampersand)
  258.   (define-key html-mode-map "\C-c\C-rl" 'html-add-normal-link-to-region)
  259.   (define-key html-mode-map "\C-c\C-rr" 'html-add-reference-to-region)
  260. )
  261.  
  262. ;;; --------------------------- buffer-local vars ----------------------------
  263.  
  264. (defvar html-link-counter-default 0)
  265. (defvar html-link-counter nil)
  266. (make-variable-buffer-local 'html-link-counter)
  267. (setq-default html-link-counter html-link-counter-default)
  268.  
  269. ;;; ------------------------------ highlighting ------------------------------
  270.  
  271. (defvar html-running-lemacs (string-match "Lucid" emacs-version)
  272.   "Non-nil if running Lucid Emacs.")
  273.  
  274. (defvar html-running-epoch (boundp 'epoch::version)
  275.   "Non-nil if running Epoch.")
  276.  
  277. (if (and html-running-epoch html-use-highlighting)
  278.     (progn
  279.       (defvar html-deemphasize-style (make-style))
  280.       (set-style-foreground html-deemphasize-style html-deemphasize-color)
  281.       (defvar html-emphasize-style (make-style))
  282.       (set-style-foreground html-emphasize-style html-emphasize-color)))
  283.  
  284. (if (and html-running-lemacs html-use-highlighting)
  285.     (progn
  286.       (defvar html-deemphasize-style (make-face 'html-deemphasize-face))
  287.       (set-face-foreground html-deemphasize-style html-deemphasize-color)
  288.       (defvar html-emphasize-style (make-face 'html-emphasize-face))
  289.       (set-face-foreground html-emphasize-style html-emphasize-color)))
  290.  
  291. (if html-use-highlighting
  292.     (progn
  293.       (if html-running-lemacs
  294.           (defun html-add-zone (start end style)
  295.             "Add a Lucid Emacs extent from START to END with STYLE."
  296.             (let ((extent (make-extent start end)))
  297.               (set-extent-face extent style)
  298.               (set-extent-data extent 'html-mode))))
  299.       (if html-running-epoch
  300.           (defun html-add-zone (start end style)
  301.             "Add an Epoch zone from START to END with STYLE."
  302.             (let ((zone (add-zone start end style)))
  303.               (epoch::set-zone-data zone 'html-mode))))))
  304.  
  305. (defun html-maybe-deemphasize-region (start end)
  306.   "Maybe deemphasize a region of text.  Region is from START to END."
  307.   (and (or html-running-epoch html-running-lemacs)
  308.        html-use-highlighting
  309.        (html-add-zone start end html-deemphasize-style)))
  310.  
  311. ;;; ----------------------------- link commands ------------------------------
  312.  
  313. (defun html-add-link (link-object)
  314.   "Add a link."
  315.   (let ((start (point)))
  316.     (setq html-link-counter (1+ html-link-counter))
  317.     (insert "<A NAME=" (format "%d" html-link-counter) 
  318.             " HREF=" link-object ">")
  319.     (html-maybe-deemphasize-region start (1- (point)))
  320.     (insert "</A>")
  321.     (push-mark)
  322.     (forward-char -4)
  323.     (html-maybe-deemphasize-region (1+ (point)) (+ (point) 4))))
  324.  
  325. (defun html-add-normal-link (link)
  326.   "Make a link.  There is no completion of any kind yet."
  327.   (interactive "sLink to: ")
  328.   (html-add-link link))
  329.  
  330. (defun html-add-normal-link-to-region (link start end)
  331.   "Make a link that applies to the current region.  Again,
  332. no completion."
  333.   (interactive "sLink to: \nr")
  334.   (save-excursion
  335.     (goto-char end)
  336.     (save-excursion
  337.       (goto-char start)
  338.       (setq html-link-counter (1+ html-link-counter))
  339.       (insert "<A NAME=" (format "%d" html-link-counter)
  340.               " HREF=" link ">")
  341.       (html-maybe-deemphasize-region start (1- (point))))
  342.     (insert "</A>")
  343.     (html-maybe-deemphasize-region (- (point) 3) (point))))
  344.  
  345. (defun html-add-reference-to-region (name start end)
  346.   "Add a reference point (a link with no reference of its own) to
  347. the current region."
  348.   (interactive "sName (or RET for numeric): \nr")
  349.   (and (string= name "")
  350.        (progn
  351.          (setq html-link-counter (1+ html-link-counter))
  352.          (setq name (format "%d" html-link-counter))))
  353.   (save-excursion
  354.     (goto-char end)
  355.     (save-excursion
  356.       (goto-char start)
  357.       (insert "<A NAME=" name ">")
  358.       (html-maybe-deemphasize-region start (1- (point))))
  359.     (insert "</A>")
  360.     (html-maybe-deemphasize-region (- (point) 3) (point))))
  361.  
  362. ;;; --------------------------- document elements ----------------------------
  363.  
  364. (defun html-add-title (title)
  365.   "Add or modify a title."
  366.   (interactive "sTitle: ")
  367.   (save-excursion
  368.     (goto-char (point-min))
  369.     (if (and (looking-at "<TITLE>")
  370.              (save-excursion
  371.                (forward-char 7)
  372.                (re-search-forward "[^<]*" 
  373.                                   (save-excursion (end-of-line) (point)) 
  374.                                   t)))
  375.         ;; Plop the new title in its place.
  376.         (replace-match title t)
  377.       (insert "<TITLE>")
  378.       (html-maybe-deemphasize-region (point-min) (1- (point)))
  379.       (insert title)
  380.       (insert "</TITLE>")
  381.       (html-maybe-deemphasize-region (- (point) 7) (point))
  382.       (insert "\n"))))
  383.  
  384. (defun html-add-header (size header)
  385.   "Add a header."
  386.   (interactive "sSize (1 or 2): \nsHeader: ")
  387.   (let ((start (point)))
  388.     (insert "<H" size ">")
  389.     (html-maybe-deemphasize-region start (1- (point)))
  390.     (insert header)
  391.     (setq start (point))
  392.     (insert "</H" size ">\n")
  393.     (html-maybe-deemphasize-region (1+ start) (1- (point)))))
  394.  
  395. (defun html-add-paragraph-separator ()
  396.   "Add a paragraph separator."
  397.   (interactive)
  398.   (let ((start (point)))
  399.     (insert "  <P>\n\n")
  400.     (html-maybe-deemphasize-region (+ start 2) (- (point) 2))))
  401.  
  402. (defun html-add-definition-list ()
  403.   "Add a definition list."
  404.   (interactive)
  405.   (let ((start (point)))
  406.     (insert "<DL>\n")
  407.     (html-maybe-deemphasize-region start (1- (point)))
  408.     (insert "<DT> ")
  409.     ;; Point goes right there.
  410.     (save-excursion
  411.       (insert "\n<DD> \n")
  412.       (setq start (point))
  413.       (insert "</DL>\n")
  414.       (html-maybe-deemphasize-region start (1- (point)))
  415.       ;; Mark goes after list -- this doesn't work.
  416.       (push-mark))))
  417.  
  418. (defun html-add-definition-entry ()
  419.   "Add a definition entry.  Assume we're at the end of a previous
  420. entry."
  421.   (interactive)
  422.   (let ((start (point)))
  423.     (insert "\n<DT> ")
  424.     (save-excursion
  425.       (insert "\n<DD> "))))
  426.  
  427. (defun html-add-plaintext ()
  428.   "Add plaintext."
  429.   (interactive)
  430.   (let ((start (point)))
  431.     (insert "<XMP>\n")
  432.     (html-maybe-deemphasize-region start (1- (point)))
  433.     (save-excursion
  434.       (insert "\n")
  435.       (setq start (point))
  436.       (insert "</XMP>\n")
  437.       (html-maybe-deemphasize-region start (1- (point)))
  438.       ;; This doesn't work.
  439.       (push-mark))))
  440.  
  441. (defun html-add-list-internal (type)
  442.   (let ((start (point)))
  443.     (insert "<" type ">\n")
  444.     (html-maybe-deemphasize-region start (1- (point)))
  445.     (insert "<LI> ")
  446.     ;; Point goes right there.
  447.     (save-excursion
  448.       (insert "\n")
  449.       (setq start (point))
  450.       (insert "</" type ">\n")
  451.       (html-maybe-deemphasize-region start (1- (point)))
  452.       ;; Mark goes after list -- this doesn't work.
  453.       (push-mark))))
  454.  
  455. (defun html-add-list ()
  456.   "Add a list."
  457.   (interactive)
  458.   (html-add-list-internal "UL"))
  459.  
  460. ;; Is this correct?  Viola doesn't seem to do anything with it.
  461. (defun html-add-menu ()
  462.   "Add a menu."
  463.   (interactive)
  464.   (html-add-list-internal "MENU"))
  465.  
  466. (defun html-add-list-or-menu-item ()
  467.   "Add a list or menu item.  Assume we're at the end of the
  468. last item."
  469.   (interactive)
  470.   (let ((start (point)))
  471.     (insert "\n<LI> ")))
  472.  
  473. (defun html-add-address ()
  474.   "Add an address."
  475.   (interactive)
  476.   (let ((start (point)))
  477.     (insert "<ADDRESS> ")
  478.     (html-maybe-deemphasize-region start (1- (point)))
  479.     (save-excursion
  480.       (setq start (point))
  481.       (insert "  </ADDRESS>\n")
  482.       (html-maybe-deemphasize-region (+ start 2) (1- (point)))
  483.       ;; Obviously this doesn't work here, so I don't
  484.       ;; see why you're being an idiot and still doing it
  485.       ;; like this....
  486.       (push-mark))))
  487.  
  488. (defun html-less-than ()
  489.   (interactive)
  490.   (insert "<"))
  491.  
  492. (defun html-greater-than ()
  493.   (interactive)
  494.   (insert ">"))
  495.  
  496. (defun html-ampersand ()
  497.   (interactive)
  498.   (insert "&"))
  499.  
  500. (defun html-real-less-than ()
  501.   (interactive)
  502.   (insert "<"))
  503.  
  504. (defun html-real-greater-than ()
  505.   (interactive)
  506.   (insert ">"))
  507.  
  508. (defun html-real-ampersand ()
  509.   (interactive)
  510.   (insert "&"))
  511.  
  512. ;;; ----------------------- html-reorder-numeric-names -----------------------
  513.  
  514. (defun replace-string-in-buffer (start end newstring)
  515.   (save-excursion
  516.     (goto-char start)
  517.     (delete-char (1+ (- end start)))
  518.     (insert newstring)))
  519.  
  520. (defun html-reorder-numeric-names (&optional reorder-non-numeric)
  521.   "Reorder the NAME fields for links in the current buffer.  The
  522. new ordering starts at 1 and increases monotonically through the buffer.
  523. If optional arg REORDER-NON-NUMERIC is non-nil, then non-numeric NAME's
  524. will also be numbered, else they won't.
  525.  
  526. Beware that doing this will possibly mess up references to specific
  527. links within this document (e.g., HREF=#12) or by other documents.
  528. This command is mainly intended for use during the initial creation
  529. stage of a document, especially when this creation involves cutting
  530. and pasting from other documents (which it shouldn't, since this is
  531. hypertext :-)."
  532.   (interactive)
  533.   (save-excursion
  534.     (goto-char (point-min))
  535.     (setq html-link-counter 0)
  536.     (while (re-search-forward "<A[ \t\n]+NAME=" (point-max) t)
  537.       (let* ((start (match-end 0))
  538.              (end (save-excursion
  539.                     (re-search-forward "[ \t\n>]" 
  540.                                        (point-max) 
  541.                                        t)
  542.                     (match-beginning 0)))
  543.              (subst (buffer-substring start end)))
  544.         (and subst
  545.              ;; Proceed only if we reorder non-numeric links or
  546.              ;; this is in fact numeric (i.e. > 0).
  547.              (or reorder-non-numeric (> (string-to-int subst) 0))
  548.              (progn
  549.                (setq html-link-counter (1+ html-link-counter))
  550.                (replace-string-in-buffer start (1- end)
  551.                 (format "%d" html-link-counter))))))))
  552.  
  553. ;;; ------------------------------- html-mode --------------------------------
  554.  
  555. (defun html-mode ()
  556.   "Major mode for editing HTML hypertext documents.  Special commands:\\{html-mode-map}
  557. Turning on html-mode calls the value of the variable html-mode-hook,
  558. if that value is non-nil.
  559.  
  560. More extensive documentation is available in the file 'html-mode.el'.
  561. The latest (possibly unstable) version of this file will always be available
  562. on anonymous FTP server ftp.ncsa.uiuc.edu in /outgoing/marca."
  563.   (interactive)
  564.   (kill-all-local-variables)
  565.   (use-local-map html-mode-map)
  566.   (setq mode-name "Html")
  567.   (setq major-mode 'html-mode)
  568.   (setq local-abbrev-table html-mode-abbrev-table)
  569.   (set-syntax-table html-mode-syntax-table)
  570.   (run-hooks 'html-mode-hook))
  571.  
  572. ;;; ------------------------------- our hooks --------------------------------
  573.  
  574. (defun html-find-file-hook ()
  575.   "Hook called from find-file-hooks.  Set html-link-counter to 
  576. the highest link value in the document (the next link created will
  577. be one greater than that) to insure unique (numeric) link ID's."
  578.   (save-excursion
  579.     (goto-char (point-min))
  580.     (while (re-search-forward "<A[ \t\n]+NAME=" (point-max) t)
  581.       (let* ((start (match-end 0))
  582.              (end (save-excursion
  583.                     (re-search-forward "[ \t\n>]"
  584.                                        (point-max)
  585.                                        t)
  586.                     (match-beginning 0)))
  587.              (subst (buffer-substring start end)))
  588.         (and subst
  589.              ;; Safe to do compare, since string-to-int passed a non-number
  590.              ;; returns 0.
  591.              (> (string-to-int subst) html-link-counter)
  592.              (setq html-link-counter (string-to-int subst)))))))
  593.  
  594. ;;; ------------------------------- hook setup -------------------------------
  595.  
  596. ;; Author: Daniel LaLiberte (liberte@cs.uiuc.edu).
  597. (defun html-postpend-unique-hook (hook-var hook-function)
  598.   "Postpend HOOK-VAR with HOOK-FUNCTION, if it is not already an element.
  599. hook-var's value may be a single function or a list of functions."
  600.   (if (boundp hook-var)
  601.       (let ((value (symbol-value hook-var)))
  602.         (if (and (listp value) (not (eq (car value) 'lambda)))
  603.             (and (not (memq hook-function value))
  604.                  (set hook-var (append value (list hook-function))))
  605.           (and (not (eq hook-function value))
  606.                (set hook-var (append value (list hook-function))))))
  607.     (set hook-var (list hook-function))))
  608.  
  609. (html-postpend-unique-hook 'find-file-hooks 'html-find-file-hook)
  610.  
  611. ;;; ------------------------------ final setup -------------------------------
  612.  
  613. (or (assoc "\\.html$" auto-mode-alist)
  614.     (setq auto-mode-alist (cons '("\\.html$" . html-mode) auto-mode-alist)))
  615.